﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Enum;

public partial class Pages_UnitInsert : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDdlState();
        }
    }

    private void FillDdlState()
    {
        StateController stateController = new StateController();
        ddlState.DataSource = stateController.GetStates();
        ddlState.DataTextField = "Acronym";
        ddlState.DataValueField = "Code";
        ddlState.DataBind();
        ddlState.Items.Insert(0, "Selecione");
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        br.com.SistemaChamados.Model.Unit unitInsert = new br.com.SistemaChamados.Model.Unit();
        UnitController controller = new UnitController();
        if (ddlState.SelectedIndex == 0)
        {
            Response.Write(@"<script language='javascript'>alert('Selecione um estado');</script>");
            ddlState.Focus();
        }
        else
        {
            if (ddlCity.SelectedIndex == 0)
            {
                Response.Write(@"<script language='javascript'>alert('Selecione uma cidade');</script>");
                ddlCity.Focus();
            }
            else
            {
                if (ddlPlace.SelectedIndex == 0)
                {
                    Response.Write(@"<script language='javascript'>alert('Selecione um lugar');</script>");
                    ddlPlace.Focus();
                }
                else
                {
                    try
                    {
                        if (controller.InsertUnit(populateObject(unitInsert)) == EnumGenericState.GENERIC_STATE_SUCCESS)
                        {
                            Response.Write(@"<script language='javascript'>alert('Estado Cadastrado com Sucesso!');</script>");
                            ClearFields();
                        }
                        else
                        {
                            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante o cadastro, por favor tente novamente!');</script>");
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(@"<script language='javascript'>alert('Ocorreu um erro no cadastro: <br>" + ex.Message +
                                           ".');</script>");
                        unitInsert = null;
                    }
                }
            }
        }
    }

    private void ClearFields()
    {
        txbName.Text = "";
        txbName.Focus();
    }

    private br.com.SistemaChamados.Model.Unit populateObject(br.com.SistemaChamados.Model.Unit unitInsert)
    {
        unitInsert.Name = txbName.Text;
        return unitInsert;
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDdlCity();
    }

    private void FillDdlCity()
    {
        CityController cityController = new CityController();
        State state = new State();
        City city = new City();
        if (Convert.ToInt32(ddlState.SelectedIndex) != 0)
        {
            state.Code = Convert.ToInt32(ddlState.SelectedValue);
            city.State = state;
            ddlCity.DataSource = cityController.GetCitiesByState(city);
            ddlCity.DataTextField = "Description";
            ddlCity.DataValueField = "Id";
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, "Selecione");
        }
        else
        {
            ClearDdlCity();
        }

    }

    private void ClearDdlCity()
    {
        for (int i = 0; i < ddlCity.Items.Count; i++)
        {
            ddlCity.Items[i].Selected = false;
        }
        ddlCity.Items[0].Selected = true;
        ddlCity.DataSource = "";
        ddlCity.DataBind();
    }

    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDdlPlace();
    }

    private void FillDdlPlace()
    {
        PlaceController placeController = new PlaceController();
        Place place = new Place();
        City city = new City();
        if (Convert.ToInt32(ddlState.SelectedIndex) != 0)
        {
            city.Id = Convert.ToInt32(ddlCity.SelectedValue);
            place.City = city;
            ddlPlace.DataSource = placeController.GetPlacesByCity(place);
            ddlPlace.DataTextField = "Address";
            ddlPlace.DataValueField = "Id";
            ddlPlace.DataBind();
            ddlPlace.Items.Insert(0, "Selecione");
        }
        else
        {
            ClearDdlPlace();
        }

    }

    private void ClearDdlPlace()
    {
        for (int i = 0; i < ddlPlace.Items.Count; i++)
        {
            ddlPlace.Items[i].Selected = false;
        }
        ddlPlace.Items[0].Selected = true;
        ddlPlace.DataSource = "";
        ddlPlace.DataBind();
    }

}